package com.cqjtu.tto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqjtu.tto.dao.mapper.TaskMapper;
import com.cqjtu.tto.dao.pojo.Task;
import com.cqjtu.tto.service.TaskService;
import com.cqjtu.tto.utils.NumUtil;
import com.cqjtu.tto.vo.ErrorCode;
import com.cqjtu.tto.vo.Results;
import com.cqjtu.tto.vo.params.TaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public Results addTask(List<TaskVo> taskVos) throws InterruptedException {
        for (TaskVo taskVo : taskVos) {
            Task task = new Task(NumUtil.nextPkId(), taskVo.getProject_stage_id(), taskVo.getUserId(),
                    taskVo.getTask_name(), taskVo.getTask_description(), null, null
                    , 0, null, null, 0);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("task_name", taskVo.getTask_name());
            Task task1 = taskMapper.selectOne(queryWrapper);
            if (task1 == null || task1.getTaskDeleted() == 1) {
                taskMapper.insert(task);
            }else{
                return Results.fail(-500, "项目任务无法重名！");
            }
        }
        return Results.success("任务添加成功！");
    }

    @Override
    public Results Endtask(Long user_id, Long task_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user_id);
        queryWrapper.eq("task_id",task_id);
        Task task = taskMapper.selectOne(queryWrapper);
        if(task == null ||task.getTaskDeleted() == 1){
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode(), "该员工任务不存在");
        }
        task.setTaskDeleted(1);
        task.setTaskStatus(1);
        if (taskMapper.updateById(task) > 0) {
            return Results.success("任务完成成功！");
        }
        return Results.fail(-500,"系统错误！");
    }

    @Override
    public Results TaskScore(Long user_id, Long task_id,Integer task_score, String task_evaluation) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user_id);
        queryWrapper.eq("task_id",task_id);
        Task task = taskMapper.selectOne(queryWrapper);
        if(task == null ||task.getTaskDeleted() == 1){
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode(), "该员工任务不存在");
        }
        task.setTaskScore(task_score);
        task.setTaskEvaluation(task_evaluation);
        if(taskMapper.updateById(task) > 0){
            return Results.success("打分评价成功!");
        }
        return Results.fail(-500,"系统错误！");
    }

    @Override
    public Results updateTask(Task task) {
        return null;
    }

    @Override
    public Results selectone() {
        return null;
    }

    @Override
    public Results selectAll(Integer project_stage_id) {
        return null;
    }

    @Override
    public Integer getEmployeeCompleteTaskCount(LocalDateTime pDateTime, LocalDateTime cDateTime, Long sysUserId) {
        return taskMapper.getEmployeeCompleteTaskCount(pDateTime, cDateTime, sysUserId);
    }
}
